package cn.com.nes.site.service.crm.customer.impl;

import cn.com.nes.common.exception.MissBusinessParameterException;
import cn.com.nes.mybatis.agent.crm.entity.CustomerStaff;
import cn.com.nes.mybatis.agent.crm.mapper.CustomerStaffMapper;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.service.crm.customer.CustomerStaffService;
import cn.com.nes.site.service.crm.customer.bo.CustomerStaffBO;
import cn.com.nes.site.service.crm.customer.dto.CustomerStaffDTO;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerStaffServiceImpl extends ServiceImpl<CustomerStaffMapper, CustomerStaff> implements CustomerStaffService  {

    /**
     * 获取客户联系人
     */
    @Override
    public List<CustomerStaffDTO> getCustomerStaffList(CustomerStaffBO customerStaffBo) {
        return this.baseMapper.getCustomerStaffList(customerStaffBo);
    }


    /**
     * 获取客户联系人
     */
    @Override
    public CustomerStaffDTO getMainCustomerStaff(String customerUUid) {
        return this.baseMapper.getMainCustomerStaff(customerUUid);
    }

    /**
     * 获取客户联系人分页
     * @param customerStaffBO
     * @return
     */
    @Override
    public Page<CustomerStaffDTO> getCustomerStaffPage(CustomerStaffBO customerStaffBO) throws Exception {
        if (StrUtil.isEmpty(customerStaffBO.getCustomerUuid())) {
            throw new MissBusinessParameterException("客户");
        }
        Page<CustomerStaffDTO> page = new Page<>(customerStaffBO.getPageid(), customerStaffBO.getPagesize());
        List<CustomerStaffDTO> customerStaffDTOS = this.baseMapper.getCustomerStaffPage(page, customerStaffBO);
        page.setRecords(customerStaffDTOS);
        return page;
    }

    /**
     * 保存客户联系人
     * @param customerStaffBO
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomerStaff saveCustomerStaff(CustomerStaffBO customerStaffBO) throws Exception {
        CustomerStaff customerStaff = getCustomerStaff(customerStaffBO.getId());
        BeanUtils.copyProperties(customerStaffBO, customerStaff);
            if(customerStaffBO.getIsHousehold()==0 && ObjectUtil.isNull(customerStaffBO.getId())) {
                if (customerStaffBO.isAdd()) {
                    customerStaff.setDeleted(DeleteEnum._0.getKey());
                    customerStaff.setGmtCreateId(customerStaffBO.getOperateUserId());
                    customerStaff.setGmtCreate(customerStaffBO.getOperateDate());
                } else {
                    customerStaff.setGmtModifiedId(customerStaffBO.getOperateUserId());
                    customerStaff.setGmtModified(customerStaffBO.getOperateDate());
                }

                this.saveOrUpdate(customerStaff);

                return customerStaff;
            }else{
                if(ObjectUtil.isNotEmpty(customerStaffBO.getId())){
                    if (customerStaffBO.isAdd()) {
                        customerStaff.setDeleted(DeleteEnum._0.getKey());
                        customerStaff.setGmtCreateId(customerStaffBO.getOperateUserId());
                        customerStaff.setGmtCreate(customerStaffBO.getOperateDate());
                    } else {
                        customerStaff.setGmtModifiedId(customerStaffBO.getOperateUserId());
                        customerStaff.setGmtModified(customerStaffBO.getOperateDate());
                    }
                    this.saveOrUpdate(customerStaff);
                    return customerStaff;
                }else{
                    if(ObjectUtil.isNull(this.baseMapper.getHouseholdCustomerStaff(customerStaffBO.getCustomerUuid()))){
                        if (customerStaffBO.isAdd()) {
                            customerStaff.setDeleted(DeleteEnum._0.getKey());
                            customerStaff.setGmtCreateId(customerStaffBO.getOperateUserId());
                            customerStaff.setGmtCreate(customerStaffBO.getOperateDate());
                        } else {
                            customerStaff.setGmtModifiedId(customerStaffBO.getOperateUserId());
                            customerStaff.setGmtModified(customerStaffBO.getOperateDate());
                        }
                        this.saveOrUpdate(customerStaff);
                        return customerStaff;
                    }
                }

            }

        return null;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomerStaffDTO getHouseholdCustomerStaff(String CustomerUuid){
     return   this.baseMapper.getHouseholdCustomerStaff(CustomerUuid);
    }
    /**
     * 删除客户联系人，可批量
     * @param customerStaffBO
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCustomerStaff(CustomerStaffBO customerStaffBO) throws Exception {
        if (CollUtil.isEmpty(customerStaffBO.getDeletedIds())) {
            return;
        }
        List<CustomerStaff> customerStaffs = (List<CustomerStaff>) this.listByIds(customerStaffBO.getDeletedIds());
        for (CustomerStaff customerStaff : customerStaffs) {
            customerStaff.setDeleted(DeleteEnum._1.getKey());
            customerStaff.setGmtModifiedId(customerStaffBO.getOperateUserId());
            customerStaff.setGmtModified(customerStaffBO.getOperateDate());
        }
        this.saveOrUpdateBatch(customerStaffs);
    }

    @Override
    public CustomerStaff getCustomerStaffList(String concat, String phone, String customerUuid) throws Exception {
        QueryWrapper<CustomerStaff> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", DeleteEnum._0.getKey());
        wrapper.eq("name", concat);
        wrapper.eq("phone", phone);
        wrapper.eq("customer_id", customerUuid);
        return this.getOne(wrapper);
    }

    private CustomerStaff getCustomerStaff(Long customerStaffId) {
        if (ObjectUtil.isNotNull(customerStaffId)) {
            CustomerStaff customerStaff = getById(customerStaffId);
            return ObjectUtil.isNull(customerStaff) ? new CustomerStaff() : customerStaff;
        }
        return new CustomerStaff();
    }

}
